﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Model;
using Ecreate.Business;
namespace Ecreate.Web.systemanage.system
{
    public partial class systemcrud : System.Web.UI.Page
    {
        protected SystemUserService service = new SystemUserService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string action = Request.QueryString["action"];
                this.hid.Value = action;
                if ("delete".Equals(action))
                {

                    string id = Request.QueryString["id"];
                    WebResult<object> result = service.Delete(id);
                    string message = " alert('{0}'); window.parent.frames[\"client\"].location=\"/systemanage/system/systemlist.aspx\"";
                    message=string.Format(message,result.Message);
                    utility.Javascript.InjectScript(this, message);
                }
                if ("edit".Equals(action))
                {
                    SystemUserService service = new SystemUserService();
                    string id = Request.QueryString["id"];
                    WebResult<t_systemuser> result = service.getByUserID(id);
                    if (result.Success)
                    {
                        this.txt_userid.ReadOnly = true;
                        this.txt_userid.Enabled = false;
                        this.txt_userid.Text = result.ResultObject.Userid;
                        this.txt_username.Text = result.ResultObject.Username;
                        this.txt_password.Text = result.ResultObject.Password;
                    }
                    else
                    {
                        this.lbl_message.Text = result.Message;
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string action = this.hid.Value;
            if (action.Equals("insert"))
            {
                if (this.txt_userid.Text.Trim() == "")
                {
                    this.lbl_message.Text = "用户ID不允许为空!";
                    this.lbl_message.Visible = true;
                    return;

                }
                char c = this.txt_userid.Text[0];
                if (!Char.IsLetter(c))
                {
                    this.lbl_message.Text = "用户ID首字符必须为字母";
                    this.lbl_message.Visible = true;
                    return;
                }
               


                t_systemuser model = new t_systemuser();
                model.Adminflag = 0;
                model.Userid = this.txt_userid.Text;
                model.Password = this.txt_password.Text;
                model.Username = this.txt_username.Text;
                WebResult<object> result = service.Insert(model);
                if (result.Success == false)
                {
                    this.lbl_message.Text = result.Message;
                    this.lbl_message.Visible = true;
                }
                else
                {
                    utility.Javascript.InjectScript(this, " window.parent.frames[\"client\"].location=\"/systemanage/system/systemlist.aspx\"");
                }
            }
            else if (action.Equals("edit"))
            {
                t_systemuser model = new t_systemuser();
                model.Adminflag = 0;
                model.Userid = this.txt_userid.Text;
                model.Username = this.txt_username.Text;
                model.Password = this.txt_password.Text;
                WebResult<object> result = service.Update(model);
                if (result.Success == false)
                {
                    this.lbl_message.Text = result.Message;
                    this.lbl_message.Visible = true;
                }
                else
                {
                    utility.Javascript.InjectScript(this, " window.parent.frames[\"client\"].location=\"/systemanage/system/systemlist.aspx\"");
                }

            }
        }
    }
}
